package com.vote.vote.controller.vote;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.vote.common.annotation.Anonymous;
import com.vote.common.constant.Constants;
import com.vote.common.core.controller.BaseController;
import com.vote.common.core.domain.AjaxResult;
import com.vote.common.core.domain.model.LoginUser;
import com.vote.common.core.page.TableDataInfo;
import com.vote.common.core.redis.RedisCache;
import com.vote.framework.web.service.TokenService;
import com.vote.vote.domain.TbVote;
import com.vote.vote.service.TbVoteService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.xml.transform.Source;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * (TbVote)表控制层
 *
 * @author makejava
 * @since 2023-09-26 10:23:31
 */
@RestController
@RequestMapping("/vote")
public class TbVoteController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(TbVoteController.class);
    /**
     * 服务对象
     */
    @Autowired
    private TbVoteService tbVoteService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private RedisCache redisCache;

    /**
     * @return com.vote.common.core.domain.AjaxResult
     * @author WanShen
     * @date 2023/9/26 16:47
     */
    @GetMapping("/limit")
    public TableDataInfo selectAll(TbVote tbVote) {
        startPage();

        return getDataTable(tbVoteService.queryList(tbVote));
    }

    @PostMapping("/UpdateVote")
    public AjaxResult UpdateVote(@RequestBody TbVote tbVote) {
//        添加视频后应该清除缓存 否则数据不刷新
        redisCache.deleteObject("allVideo");
//        其他的缓存
        redisCache.deleteObject("tbVotes");
        BaseMapper<TbVote> baseMapper = tbVoteService.getBaseMapper();
        int i = baseMapper.updateById(tbVote);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("totals",i);
        map.put("tbVote",tbVote);
        return AjaxResult.success(Constants.SUCCESS, map);
    }

    @GetMapping("/deleteVote")
    public AjaxResult deleteVote(Integer id) {
        BaseMapper<TbVote> baseMapper = tbVoteService.getBaseMapper();

        return AjaxResult.success(baseMapper.deleteById(id));
    }

    /**
     * 添加视频
     *
     * @param tbVote
     * @return com.vote.common.core.domain.AjaxResult
     * @author WanShen
     * @date 2023/10/7 19:34
     */
    @PostMapping("/insertVote")
    public AjaxResult insertVote(@RequestBody TbVote tbVote, HttpServletRequest request) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        //添加用户的id
        tbVote.setvUid(Math.toIntExact(loginUser.getUser().getUserId()))
//                设置创建时间
                .setCreateTime(new Date())
//        设置视频状态 待审核
                .setvState(0).setvCount(0).setEmail("");
//        TODO 等待加入订单相关;

        tbVoteService.insertVote(tbVote);
//        添加视频后应该清除缓存 否则数据不刷新
        boolean allVideo = redisCache.deleteObject("allVideo");

        return AjaxResult.success().put("cache", allVideo);
    }

    /**
     * 排行榜信息
     *
     * @return com.vote.common.core.domain.AjaxResult
     * @author WanShen
     * @date 2023/10/8 14:45
     */
    @GetMapping("/ranking")
    @Anonymous
    public AjaxResult getRanking() {

        return AjaxResult.success(tbVoteService.getRanking());
    }

    @GetMapping("/getAllVideo")
    @Anonymous
    public AjaxResult getAllVideo() {
        List<TbVote> tbVotes = new ArrayList<>();
        //redis中查询
        tbVotes = redisCache.getCacheList("allVideo");
        //说明没有数据 去数据库
        if (tbVotes.size() < 1) {
            //查询审核通过的视频
            tbVotes = tbVoteService.queryList(new TbVote().setvState(1));
            //将数据保存
            redisCache.setCacheList("allVideo", tbVotes);
        }

        //否则就是有数据
        return AjaxResult.success(tbVotes);
    }

    @GetMapping("/getVideoByUid")
    public AjaxResult getVideoByUid(Integer uid) {

        TbVote tbVote = new TbVote();
        tbVote.setvUid(uid);

        return AjaxResult.success(tbVoteService.queryList(tbVote));
    }

}

